//
//  MineBottomCateView.swift
//  LeGou
//
//  Created by lxm on 2022/7/29.
//

import UIKit

class MineBottomCateView: UIView {

    //MARK: --------------------属性--------------------
    var data : [(title : String, icon : String, id : Int)] {
        get {
            if AppHelper.appVerify() {
                return [
                    (title : "实名认证", icon : "mine_smrz", id : 1),
                    (title : "成为合伙人", icon : "mine_cwhhr", id : 2),
                    (title : "第三方账户", icon : "mine_dsfzh", id : 3),
                    (title : "收货地址", icon : "mine_shdz", id : 4)
                ]
            } else {
                return [
                    (title : "实名认证", icon : "mine_smrz", id : 1),
                    (title : "成为合伙人", icon : "mine_cwhhr", id : 2),
                    (title : "第三方账户", icon : "mine_dsfzh", id : 3),
                    (title : "收货地址", icon : "mine_shdz", id : 4),
                    (title : "商家收款码", icon : "mine_sjskm", id : 5),
                    (title : "商家入驻", icon : "mine_sjrz", id : 5)
                ]
            }
        }
    }
    
    var onClick : ((_ item : (title : String, icon : String, id : Int))->())?

    //MARK: --------------------UI控件--------------------
    let title = ZMUtil.label(title: "常用功能", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 16, weight: .medium))

//    let wholeStack = ZMUtil.stackView(axis: .vertical, spacing: 12)

    let conentView = UIView()
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        backgroundColor = .white
        layer.cornerRadius = 12
        
        // 一行多少个
        let rowCount = 4
        addSubview(title)
        let w = (SCREEN_W - 32) / CGFloat(rowCount)
        for i in 0 ..< data.count {
            let item = data[i]
            let row = CGFloat(i / 4)
            let col = CGFloat(i % 4)
            let itemView = ItemView(frame: CGRect(x: w * col, y: row * (ItemView.itemHeight + 12), width: w, height: ItemView.itemHeight))
            itemView.title.text = item.title
            itemView.icon.image = UIImage.init(named: item.icon)
            itemView.alpha = 0.7
            itemView.onClick = { [weak self] in
                guard let self = self else {return}
                if let onClick = self.onClick {
                    onClick(item)
                }
            }
            conentView.addSubview(itemView)
        }
        addSubview(conentView)
    }
    
    func addConstraints() {
        title.snp.makeConstraints { make in
            make.top.left.equalTo(self).offset(12)
        }
        
        conentView.snp.makeConstraints { make in
            make.top.equalTo(title.snp.bottom).offset(16)
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.bottom.equalTo(self).offset(-12)
            let row = CGFloat((self.data.count - 1) / 4 + 1)
            make.height.equalTo(row * (ItemView.itemHeight + 12))
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    //MARK: --------------------delegate--------------------
}

extension MineBottomCateView {
    class ItemView : UIView {
        
        var onClick : (()->())?
        
        static let itemHeight : CGFloat = 45
        
        let title = ZMUtil.label(title: "商城订单", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .regular))
        
        let icon = ZMUtil.imageView(imageName: "right_red")

        override init(frame: CGRect) {
            super.init(frame: frame)
            setUI()
        }
        
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            setUI()
        }
        
        func setUI() {
            let t = UITapGestureRecognizer(target: self, action: #selector(tap))
            addGestureRecognizer(t)
            addSubview(icon)
            addSubview(title)
            title.textAlignment = .center
            icon.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.centerX.equalToSuperview()
            }
            
            title.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(8)
                make.top.equalTo(icon.snp.bottom).offset(4)
                make.centerX.equalToSuperview()
            }
        }
        
        @objc func tap() {
            if let onClick = onClick {
                onClick()
            }
        }
    }
}
